import pandas as pd

from pandasgui import show


class Index_cosAnalysis:
    def __init__(self):
        self.codes = ['600036', '002594', '300006', '000905', '002607']

    def analysis(self):
        index_pd = pd.read_csv('./data/sh000001.csv')
        index_pd['return'] = index_pd['close'].pct_change()
        res_array = []
        for item in self.codes:
            print(item)
            df = pd.read_csv('./data_return/' + item + '_return.csv')
            # 计算描述性统计数据
            mean_return = df['return'].mean()
            median_return = df['return'].median()
            std_return = df['return'].std()
            max_return = df['return'].max()
            min_return = df['return'].min()

            # 打印结果
            print(f"均值: {mean_return}")
            print(f"中位数: {median_return}")
            print(f"标准差: {std_return}")
            print(f"最大值: {max_return}")
            print(f"最小值: {min_return}")

            # 假设另一个变量是index_return
            covariance = df['return'].cov(index_pd['return'])
            correlation = df['return'].corr(index_pd['return'])

            # 打印结果
            print(f"协方差: {covariance}")
            print(f"相关系数: {correlation}")
            res_array.append(
                [item, mean_return, median_return, std_return, max_return, min_return, covariance, correlation])
            # 转换为 DataFrame
            # columns = ['Stock Code', 'Mean Return', 'Median Return', 'Std Return', 'Max Return', 'Min Return', 'Covariance', 'Correlation']
        #change res_array to dataframe
        columns = ['Stock Code', 'Mean Return', 'Median Return', 'Std Return', 'Max Return', 'Min Return', 'Covariance',
                   'Correlation']
        df = pd.DataFrame(res_array, columns=columns)
        df.to_csv('./Step3_analysis.csv')
        # 显示数据集
        show(df)
        # import os
        # os.environ['APPDATA'] = ""


if __name__ == '__main__':
    Index_cosAnalysis().analysis()
